<template>
      <Form :model="form" :rules="rules" mode="twocolumn" ref="formRef">
        <FormItem :label="$t('父级机构','Parent')" prop="pid" :required="form.sn!='01'">
          <CategoryPicker
              v-model="form.pid"
              :option="branchOptions"
              :disabled="form.id!=null"
          />
        </FormItem>
        <FormItem :label="$t('机构代码','Code')" prop="code" required>
          <Input type="text" v-model="form.code" />
        </FormItem>
        <FormItem :label="$t('名称','Code')" prop="name" required>
          <Input type="text" v-model="form.name"/>
        </FormItem>
        <FormItem :label="$t('全称','FullName')" prop="fullName" required>
          <Input type="text" v-model="form.fullName"/>
        </FormItem>
        <FormItem :label="$t('类型','Type')" prop="type" required>
          <Select v-model="form.type" dict="BranchType" filterable></Select>
        </FormItem>
        <FormItem :label="$t('负责人','Manager')" prop="leader">
          <Input type="text" v-model="form.leader"/>
        </FormItem>
        <FormItem :label="$t('联系电话','Phone')" prop="phone">
          <Input type="text" v-model="form.phone"/>
        </FormItem>
        <FormItem :label="$t('电子邮箱')" prop="email">
          <Input type="text" v-model="form.email"/>
        </FormItem>
        <FormItem :label="$t('传真','Fax')" prop="fax">
          <Input type="text" v-model="form.fax"/>
        </FormItem>
        <FormItem :label="$t('联系地址','Address')" prop="address">
          <Input type="text" v-model="form.address"/>
        </FormItem>
        <FormItem :label="$t('备注信息','Remark')" prop="remark">
          <Input type="text" v-model="form.remark"/>
        </FormItem>
        <FormItem :label="$t('邮政编码','ZipCode')" prop="zipcode">
          <Input type="text" v-model="form.zipcode"/>
        </FormItem>
      </Form>
</template>
<script setup>
import { ajax, util} from 'framework'
import {reactive,ref} from 'vue'
import {message} from 'heyui'
import {$t} from 'i18n'
const {params} = defineProps({
  params:Object
})
const {branchOpts:branchOptions}=params
delete params.branchOpts
const formRef=ref(null)
const form= reactive({
  sn: null,
  psn: null,
  code: null,
  name: null,
  fullName: null,
  type: null,
  leader: null,
  phone: null,
  email: null,
  fax: null,
  address: null,
  remark: null,
  zipcode: null,
  jf: null,
})
const rules={}

const branchOption=reactive({
  keyName: 'sn',
  parentName: 'psn',
  titleName: 'name',
  dataMode: 'list',
  datas: []
})
const emits=defineEmits(['load'])

ajax.get("branch/list").then(r => {
  branchOption.datas = r.data;
})

let isAdd=false
if(params.id){
  emits('load',true)
  ajax.get("branch/detail", params).then(r => {
    Object.assign(form,r.data)
    emits('load',false)
  })
}else{
  isAdd=true
}
function submit(loading,success){
  let validResult = formRef.value.valid();
  if (validResult.result) {
    loading.value = true
    ajax.post(isAdd ? 'branch/add' : 'branch/save', form).then(r => {
      message($t("已保存",'Saved'))
      success(r.data)
    }).finally(e => {
      loading.value = false
    })
  }
}
defineExpose({submit})
</script>

